package com.example.config.Utility;

import org.springframework.beans.factory.annotation.Value;

import javax.print.*;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.Size2DSyntax;
import javax.print.attribute.standard.JobName;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.OrientationRequested;
import javax.print.event.PrintJobEvent;
import javax.print.event.PrintJobListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Locale;

public class PrintUtil {
    private static final long j = 1;

    @Value("${Image_path.path}")
    private String this_path;

    public void printImage(String ImageName) {
        // 构建打印请求属性集
        DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
        // 设置打印格式
        PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
        // 查找所有的打印服务
        PrintService[] printServices = PrintServiceLookup.lookupPrintServices(flavor, pras);
        // 因为只是测试，这里我直接用第一个了
//        for (PrintService printServicess : printServices){
//            System.out.println(printServicess);
//        }
        System.out.println(printServices[3]);
        PrintService printService = printServices[3];
        String printName = printService.getName();
        // 判断当前打印机状态
//        Winspool.PRINTER_INFO_2 printerInfo2 = WinspoolUtil.getPrinterInfo2(printName);
//        if (printerInfo2.Status != 0) {
//            return;
//        }
        File picFile = new File(ImageName);
        if (!picFile.exists() || !picFile.isFile()) {
            System.out.println("文件不存在");
            return;
        }
        String fileName = picFile.getName();
        // 获取图片后缀名，设置打印文件格式
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        switch (suffix) {
            case "jpg":
            case "jpeg":
                flavor = DocFlavor.INPUT_STREAM.JPEG;
                break;
            case "png":
                flavor = DocFlavor.INPUT_STREAM.PNG;
                break;
            default:
                break;
        }
        // 设置打印文件名
        pras.add(new JobName(fileName, Locale.CHINA));
        // 设置打印方向
        pras.add(OrientationRequested.LANDSCAPE);
        // 设置打印区域
        MediaPrintableArea mp = new MediaPrintableArea(0, 100, 230, 203, Size2DSyntax.MM);
        pras.add(mp);
        try {
            FileInputStream fis = new FileInputStream(picFile);
            HashDocAttributeSet das = new HashDocAttributeSet();
            for (long i = 0; i < j; i++) {
                // 使用打印服务生成打印任务
                DocPrintJob job = printService.createPrintJob();
                // 获取需要打印的文档类doc
                SimpleDoc doc = new SimpleDoc(fis, flavor, das);
                // 监听打印任务
                job.addPrintJobListener(new PrintJobListener() {
                    @Override
                    public void printDataTransferCompleted(PrintJobEvent pje) {
                        System.out.println("成功发送任务：" + fileName);
                    }

                    @Override
                    public void printJobCompleted(PrintJobEvent pje) {
                        System.out.println("打印任务完成！");
                    }

                    @Override
                    public void printJobFailed(PrintJobEvent pje) {
                        System.out.println("打印任务失败：" + fileName);
                    }

                    @Override
                    public void printJobCanceled(PrintJobEvent pje) {
                        System.out.println("取消打印任务！");
                    }

                    @Override
                    public void printJobNoMoreEvents(PrintJobEvent pje) {
                        System.out.println("没有更多方法提供打印机调用！");
                    }

                    @Override
                    public void printJobRequiresAttention(PrintJobEvent pje) {
                        System.out.println("纸张已用完");
                    }
                });
                job.print(doc, pras);
            }
        } catch (PrintException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
